import numpy as np

years = np.array([i for i in range(2000, 2022)])

max_year = np.max(years)
min_year = np.min(years)
tmp_years = (years - min_year) / (max_year - min_year)

prices = np.array(
    [10000, 11000, 12000, 13000, 14000, 12000, 13000, 16000, 18000, 20000, 19000, 22000, 24000, 23000, 26000, 35000,
     30000, 40000, 45000, 52000, 50000, 60000])
max_price = np.max(prices)
min_price = np.min(prices)
tmp_prices = (prices - min_price) / (max_price - min_price)

epoch = 100000

k = 1
b = 0
lr = 0.001

for e in range(epoch):
    for x, label in zip(tmp_years, tmp_prices):
        pre = k * x + b
        loss = (pre - label) ** 2

        delta_k = 2 * (k * x + b - label) * x
        delta_b = 2 * (k * x + b - label)

        k = k - delta_k * lr
        b = b - delta_b * lr
print(f"k={k}, b={b}")
while True:
    year = (float(input("请输入年份: ")) - min_year) / (max_year-min_year)
    print("预测房价: ", (k * year + b) * (max_price - min_price) + min_price)
